<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\CommonModel;

/**
 * by：wl
 * date：20181112
 * fun:后台菜单管理
 */
class Menu extends Base
{
    public function index(){
        $list = Db::query('select * from admin_menu where pid=0 and status != 2 order by rank desc ');  
        foreach ($list as $k => $v) {
            $id = $v['id'];
            $list[$k]['data'] = Db::query("select * from admin_menu where pid=$id and status != 2 order by rank desc");
            foreach ($list[$k]['data'] as $kk => $vv) {
      
                if($vv['controller'] == ''){
                    $ids = $vv['id'];
                    $list[$k]['data'][$kk]['data2'] =  Db::query("select * from admin_menu where pid=$ids and status != 2 order by rank desc");
                }else{
                    $list[$k]['data'][$kk]['data2'] = '';
                }
            }

        }
        $this->assign("list",$list);
        // print_r($list);

        //总数据
        $count = Db::query('select count(*) as count from admin_menu where status != 2');
        $this->assign("count",$count[0]);

        //可选分级
        $fenji_list = Db::query('select * from admin_menu where controller = ""');
        $this->assign("fenji_list",$fenji_list);

    	return view();
    }
    public function add_edit(){
        $data = $_GET;
        // print_r($data);exit;
        $res = CommonModel::write($data,'admin_menu');
        if($res){
            echojson(true,1,'操作成功');
        }else{
            echojson(false,0,'操作失败');
        }
    }
    public function del(){
        $id = $_POST['id'];
        // print_r($_POST);exit;
        
        $res = Db::table('admin_menu')
                        ->where('id', $id)
                        ->update([
                            'status' => 2,
                        ]);
        if($res){
            echojson(true,1,'操作成功');
        }else{
            echojson(false,0,'操作失败');
        }
    }

    
   
}

